<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学员姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入学员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="阶段" prop="stage">
        <el-input
          v-model="queryParams.stage"
          placeholder="请输入阶段"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级编号" prop="classesId">
        <el-input
          v-model="queryParams.classesId"
          placeholder="请输入班级编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月份" prop="month">
        <el-input
          v-model="queryParams.month"
          placeholder="请输入月份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出勤状态" prop="attendance">
        <el-input
          v-model="queryParams.attendance"
          placeholder="请输入出勤状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teaching:attendance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teaching:attendance:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teaching:attendance:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teaching:attendance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <button @click="exportExcel">导出excel</button>
    <div style="overflow: auto; height: 100%; width: 100%;">
      <table id="attendanceTable">
        <thead>
          <tr>
            <th>{{attendanceMonth.month}}</th>
            <th rowspan="3"></th>
            <th v-for="i in attendanceMonth.days">{{i}}</th>
          </tr>
          <tr>
            <td>星期</td>

            <td v-for="i in attendanceMonth.weekdays">{{i}}</td>
          </tr>
          <tr>
            <td>课表</td>

            <td v-for="i in attendanceMonth.teachingPlans">{{i}}</td>
          </tr>
        </thead>
        <tbody>
        <template v-for="stu in attendanceMonth.list">
        <tr >
          <td rowspan="3">{{stu.name}}</td>
          <td>早</td>
          <td v-for="att in stu.attendance[0].status" ><span :class="{green:att=='√',red:att=='-'}">{{att}}</span></td>
        </tr>
        <tr >
          <td>中</td>
          <td v-for="att in stu.attendance[1].status">{{att}}</td>
        </tr>
        <tr >
          <td>晚</td>
          <td v-for="att in stu.attendance[2].status">{{att}}</td>
        </tr>
        </template>
        </tbody>
      </table>
    </div>

    <!-- 添加或修改考勤对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学员姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入学员姓名" />
        </el-form-item>
        <el-form-item label="阶段" prop="stage">
          <el-input v-model="form.stage" placeholder="请输入阶段" />
        </el-form-item>
        <el-form-item label="班级编号" prop="classesId">
          <el-input v-model="form.classesId" placeholder="请输入班级编号" />
        </el-form-item>
        <el-form-item label="月份" prop="month">
          <el-input v-model="form.month" placeholder="请输入月份" />
        </el-form-item>
        <el-form-item label="出勤状态" prop="attendance">
          <el-input v-model="form.attendance" placeholder="请输入出勤状态" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>

</template>
<style>
#attendanceTable {
  /*width: 100%;*/
  /*border-collapse: collapse;*/
  border-spacing: 0;
  border: 1px solid black;
}
#attendanceTable td {
  text-align: left;
  padding: 8px;
  border: 1px solid black;
}
#attendanceTable th {
  text-align: left;
  padding: 8px;
  width: 200px;
  border: 1px solid black;
  background-color: #f2f2f2;
}
.green {
  color: green;
}

.red {
  color: red;
}
</style>
<script>
import { listAttendance,listAllAttendance, getAttendance, delAttendance, addAttendance, updateAttendance } from "@/api/teaching/attendance";
import XLSX from 'xlsx';

export default {
  name: "Attendance",
  data() {
    return {
      attendanceMonth: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤表格数据
      attendanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        stage: null,
        classesId: null,
        month: null,
        attendance: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList()
  },
  methods: {
    exportExcel() {
      const XLSX = require('xlsx');
      console.log('Exporting Excel');
      // 获取表格元素
      const table = document.querySelector('table');

      // 将表格转换为工作簿对象
      const workbook = XLSX.utils.table_to_book(table);

      // 将工作簿对象转换为Excel文件的二进制数据
      const excelData = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });

      // 创建Blob对象
      const blob = new Blob([excelData], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });

      // 创建下载链接并点击下载
      const url = URL.createObjectURL(blob);
      const link = document.createElement('a');
      link.href = url;
      link.download = 'table.xlsx';
      link.click();
    },
    /** 查询考勤列表 */
    getList() {
      this.loading = true;
      listAllAttendance(this.queryParams).then(response => {
        this.attendanceMonth = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        stage: null,
        classesId: null,
        month: null,
        attendance: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考勤";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAttendance(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考勤";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAttendance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttendance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除考勤编号为"' + ids + '"的数据项？').then(function() {
        return delAttendance(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teaching/attendance/export', {
        ...this.queryParams
      }, `attendance_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
